package edu.ccut.saturn.studio.dialog.actiondialog;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnActionNode;

public class SaturnActionDialog extends TitleAreaDialog {
	
	private Text idText;
	private Text nameText, expressionText, descriptionText;
	
	private Label nameLabel, expressionLabel, descriptionLabel, browser;
	
	private SaturnActionNode saturnActionNode;
	
	private boolean serach = false;
	
//	private Map<String,Document> tempMap = new HashMap<String,Document>();
	
	public SaturnActionDialog(Shell parentShell, SaturnActionNode saturnActionNode) {
		super(parentShell);
		this.saturnActionNode = saturnActionNode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING); 
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Label idLabel = new Label(container, SWT.NONE);
		idLabel.setText(Messages.getString("SaturnActionDialog.ID")); //$NON-NLS-1$

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.horizontalIndent = 10;
		gd_idText.verticalIndent = 10;
		idText.setLayoutData(gd_idText);
		
		new Label(container, SWT.NONE);
		
		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 10;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnActionDialog.name"));  //$NON-NLS-1$
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_nameText.horizontalIndent = 10;
		gd_nameText.verticalIndent = 10;
		nameText.setLayoutData(gd_nameText);
		new Label(container, SWT.NONE);
		
		expressionLabel = new Label(container, SWT.NONE);
		final GridData gd_expressionLabel = new GridData();
		gd_expressionLabel.verticalIndent = 10;
		expressionLabel.setLayoutData(gd_expressionLabel);
		expressionLabel.setText(Messages.getString("SaturnActionDialog.expression"));  //$NON-NLS-1$
		
		expressionText = new Text(container, SWT.BORDER);
		expressionText.setTextLimit(150);
		final GridData gd_expressionText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_expressionText.horizontalIndent = 10;
		gd_expressionText.verticalIndent = 10;
		expressionText.setLayoutData(gd_expressionText);
		
		browser = new Label(container, SWT.NONE);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
//				FilterDialogObserver dialog1 = new FilterDialogObserver(Display.getDefault().getShells()[0], saturnActionNode);
//				dialog1.setTitle(Messages.getString("SaturnActionDialog.ChooseKeyValue")); //$NON-NLS-1$
//				dialog1.setMap(tempMap);
//				if (dialog1.open() == Window.OK) {
//					String expression=(String)dialog1.getFirstResult();
//					String expressions[]=expression.split("--");
//					expression=expressions[1].trim();
//					expressionText.setText(expression);
//					tempMap = dialog1.getMap();
//				}
				OneCompontentSearchDialog searchDialog = new OneCompontentSearchDialog(Display.getDefault().getShells()[0], saturnActionNode);
				searchDialog.setTitle(Messages.getString("SaturnActionDialog.ChooseKeyValue")); //$NON-NLS-1$
				if (searchDialog.open() == Window.OK) {
					SearchResult expression = (SearchResult)searchDialog.getFirstResult();
					expressionText.setText(expression.getKey());
					
				}
			}
		});
		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}
			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		final GridData gd_browser = new GridData();
		gd_browser.verticalIndent = 10;
		browser.setLayoutData(gd_browser);
		
		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP, false, false);
		gd_descriptionLabel.verticalIndent = 14;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnActionDialog.description"));  //$NON-NLS-1$
		
		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.FILL, true, true,2,1);
		gd_descriptionText.heightHint = 53;
		gd_descriptionText.verticalIndent = 10;
		gd_descriptionText.horizontalIndent = 10;
		descriptionText.setLayoutData(gd_descriptionText);
		
		idText.setText(saturnActionNode.getId().trim());
		nameText.setText(saturnActionNode.getName());
		expressionText.setText(saturnActionNode.getKey());
		descriptionText.setText(saturnActionNode.getDescription());
		
		/**
		 * 名称不可为空
		 * 
		 * */
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if("".equals(nameText.getText().trim())){ //$NON-NLS-1$
					setErrorMessage(Messages.getString("SaturnActionDialog.NameCanontBeEmpty"));  //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		setMessage(Messages.getString("SaturnActionDialog.nameLengthLimite")); //$NON-NLS-1$
		setTitle(Messages.getString("SaturnActionDialog.setAttribute")); //$NON-NLS-1$
		
		return area;
	}
	
	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnActionNode.setDescription(descriptionText.getText().trim());
			saturnActionNode.setKey(expressionText.getText().trim());
			saturnActionNode.setName(nameText.getText().trim());
		}
		super.buttonPressed(buttonId);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	protected Point getInitialSize() {
		return new Point(530, 300);
	}
	
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
	
	class GUIUpdater implements Runnable {
		private IProgressMonitor monitor;

		GUIUpdater(IProgressMonitor monitor) {
			this.monitor = monitor;
		}

		public void run() {
			for (int i = 0; i < 34; i++) {
				try {
					Thread.sleep(500);
					this.monitor.worked(5);
					if (serach == true) {
						this.monitor.worked(100);
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
